package com.xc.h05.controller.wutao;

import com.github.pagehelper.PageInfo;
import com.xc.h05.entity.Chargetype;
import com.xc.h05.service.wutao.ChargetypeService;
import com.xc.h05.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/chargetype")
public class ChargetypeController {
    @Autowired
    private ChargetypeService chargetypeService;
    //查询所有
    @GetMapping("/findAll")
    public List<Chargetype> findAll(){
        return chargetypeService.findAll();
    }
    //增加
    @PostMapping("/add")
    public R add(@RequestBody Chargetype chargetype) {
        int add = chargetypeService.add(chargetype);
        if (add != 0) {
            return R.ok(chargetype);
        } else {
            return R.fail("添加失败");
        }
    }
    //根据ID删除
    @DeleteMapping("/delete")
    public int delete(int id){
        return chargetypeService.delete(id);
    }
    //更改
    @PutMapping("/update")
    public int update(@RequestBody  Chargetype chargetype){
        return chargetypeService.update(chargetype);
    }

    //根据id批量删除
    @PostMapping("/deleteByIds")
    public R deleteByIds(@RequestBody List<Integer> ids) {
        int i = chargetypeService.deleteByIds(ids);
        if (i >= 0) {
            return R.ok ("批量删除成功");
        }else return R.fail ("批量删除失败");
    }
    //    分页查询所有
    @GetMapping("/getAll")
    public R getAll(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
            Chargetype chargetype
    ) {
        PageInfo<Chargetype> pageInfo = chargetypeService.findByPage(pageNum, pageSize,chargetype);
        return R.ok(pageInfo);
    }
    //根据id查询
    @GetMapping("/findById")
    public Chargetype findById(int id){
        return chargetypeService.findById(id);
    }
}
